<?php
/**
 * @Copyright (c) 2011, 新浪网运营部-网络应用开发部
 * All rights reserved.
 * 信息提示基类
 * @author          wangxin <wangxin3@staff.sina.com.cn>
 * @time            2011/3/2 11:48
 * @version         Id: 0.9
*/

class BaseModelMessage {
    
    private function __construct() {
        return;
    }

    public static function showSucc($msg, $data = array(), $otherData = array(), $url = '', $t = 3) {
        self::message(0, $msg, $data, $url, $t, $otherData);
    }

    public static function showError($msg, $data = array(), $code = 11, $url = '', $t = 3) {
        self::message($code, $msg, $data, $url, $t);
    }
    
    private static function message($code, $msg, $data, $url, $t, $otherData = array()) {
    	$format = strtolower(BaseModelParams::get('format'));
        if ($format == 'xml') {
            @header("Content-Type: text/xml");
        } elseif ($format == 'json') {
            @header('Content-Type: application/json');
        }
        if ($format == 'json') {
    		$outArr = array();
            if (!is_array($msg)) {
		        $outArr['result']['status']['code'] = $code;
		        $outArr['result']['status']['msg'] = $msg;
		        $outArr['result']['encoding'] = $oe;
                if (is_array($otherData)) {
                    foreach ($otherData as $k=>$v) {
                        if (!in_array($k, array('status', 'encoding', 'data'))) {
                            $outArr['result'][$k] = $v;
                        }
                    }
                }
		        $outArr['result']['data'] = $data;
            } else {
            	$outArr = $msg;
            }
            echo json_encode($outArr);
            exit;
        }
        if (defined('QUEUE')) {
            echo $msg . "\n";
            return;
        }
        $tpl = new BaseView();
        $tpl->assign('msg', $msg);
        $tpl->assign('url', $url);
        $tpl->assign('t', $t);
        if ($code == 0) {
        	$tpl->display('message/message.html');
        } else {
        	$tpl->display('message/error.html');
        }
        exit;
    }
    
}
?>
